package net.learn2develop.GetVcb;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBAdapter {
	/****************TABLE and property datebase**************************/
	public static final String KEY_ROWID = "_id";
	public static final String KEY_COUNTRY = "country";
	public static final String KEY_BUY = "buy";
	public static final String KEY_SELL = "sell";
	public static final String KEY_DATE = "date1";
	public static final String KEY_TIME = "time1";
	private static final String DATABASE_NAME="MyDB";
	private static final String DATABASE_TABLE="VCB";
	private static final int DATABESE_VERSION = 1;	
	private static final String DATABASE_CREATE = 
		"create table VCB (_id integer primary key autoincrement, "
		+ "country text not null, buy text not null, sell text not null, date1 text not null, time1 text not null);";
	/**********************Declaration datebase***************************/
	private final Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;	
	
	public DBAdapter (Context ctx){
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);	
		
	}
	/*****************Class Databasehelper*********************************/
	private static class DatabaseHelper extends SQLiteOpenHelper{

		public DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABESE_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXITS VCB");
			onCreate(db);
		}
	}
	public DBAdapter open(){
		db = DBHelper.getWritableDatabase();
		return this;
	}
	//--Close database
	public void close(){
		DBHelper.close();
	}
	//--insert the contacts into databases--
	public long insertContact(String country, String buy, String sell,String date, String time){
		ContentValues initialValues = new ContentValues();
		initialValues.put(KEY_COUNTRY, country);
		initialValues.put(KEY_BUY, buy);
		initialValues.put(KEY_SELL, sell);
		initialValues.put(KEY_DATE, date);
		initialValues.put(KEY_TIME, time);
		return db.insert(DATABASE_TABLE, null, initialValues);
	}
	//--deletes a particular contact--
	public boolean deleteContact(long rowId){
		return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId,null) > 0;
	}
	//------------delete all
	public boolean deleteAllContact(){
		return db.delete(DATABASE_TABLE, "1" ,null) > 0;
	}
	/**************get Filter**************************/
	public Cursor getFilter(String selection,String[] SelectionArgs, String groupBy, String having, String order){
		Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
				KEY_COUNTRY	,KEY_BUY, KEY_SELL,KEY_DATE,KEY_TIME},selection,SelectionArgs,
				groupBy,having,order,null);
		return mCursor;
	}
	//--retrieves all the contact
	public Cursor getAllContacts(){
	//	return db.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_NAME,
		//		KEY_EMAIL},null, null, null, null, null);
		return db.query(DATABASE_TABLE, new String[]{KEY_ROWID,KEY_COUNTRY,KEY_BUY,KEY_SELL,KEY_DATE,KEY_TIME}, null, null, null, null, null);
	}
	//--retrieves a particular the contact
	public Cursor getContact(long rowId) throws SQLException{
		Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
						KEY_COUNTRY	,KEY_BUY, KEY_SELL,KEY_DATE,KEY_TIME},KEY_ROWID+"="+rowId,null,
						null,null,null,null);
		if(mCursor != null){
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	//--Update a contact
	public boolean updateContact(long rowId, String country,String buy,String sell,String date, String time){
		ContentValues args=new ContentValues();
		args.put(KEY_COUNTRY, country);
		args.put(KEY_BUY, buy);
		args.put(KEY_SELL,sell);
		args.put(KEY_DATE,date);
		args.put(KEY_TIME,time);
		return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null )>0;
	}
	
}
